package com.yl.chainStore.entity.pay

import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.yl.chainStore.common.Constants
import com.yl.chainStore.common.enums.Status
import com.yl.chainStore.pojo.vo.customer.CustomerLeftJoinInfo
import com.yl.common.annotation.NoArg
import com.yl.common.getContext
import java.util.*
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * <p>
 *
 * </p>
 *
 * @author cxw
 * @since 2021-07-09
 */
@NoArg
@TableName("pay_customer_order")
data class PayCustomerOrder(

	/**
	 * 订单id
	 */
	@TableId(value = "id", type = IdType.AUTO)
	var id: Long = 0,

	/**
	 * 订单编号
	 */
	@TableField("order_num")
	var orderNum: String? = null,

	/**
	 * 公司id
	 */
	@TableField("company_id")
	var companyId: Long = getContext().companyId,


	/**
	 * 客户id
	 */
	@TableField("customer_id")
	var customerId: Long,

	/**
	 * 部门id
	 */
	@TableField("dept_id")
	var deptId: Long = getContext().subCompanyId,

	@TableField(exist = false)
	var subCompanyName:String ?= null,

	/**
	 * 创建人
	 */
	@TableField("create_user_id")
	var createUserId: Long = getContext().userId,

	@TableField(exist = false)
	var createUserName: String? = null,

	/**
	 * 创建时间
	 */
	@TableField("create_time")
	var createTime: Date = Date(),

	/**
	 * 主要负责人（医生） 暂时不用了
	 */
	@TableField("master_user_id")
	var masterUserId: Long? = null,

	@TableField(exist = false)
	var masterUserName: String? = null,
	/**
	 * 次要负责人（咨询师）  暂时不用
	 */
	@TableField("secondary_user_id")
	var secondaryUserId: Long? = null,

	/**
	 * 状态(付款状态) NEW_ADD 新增
	 *              WAIT_PAY 待付款  （下单后）
	 *              PAYING    付款部分  欠款
	 *              END         付款完成
	 */
	@TableField("status")
	var status: Status = Status.NEW_ADD,

	/**
	 * 项目执行状态 NEW_ADD 新增        全部未处理
	 *              ONGOING  处理中  底下明细未处理完成
	 *              END     结束      全部处理结束
	 */
	@TableField("sick_status")
	var sickStatus: Status = Status.NEW_ADD,

	/**
	 * 消耗积分
	 */
	@TableField("integral_discount")
	var integralDiscount: Long = 0,


	/**
	 * 抵扣（手动优惠金额）金额
	 */
	@TableField("discount_money")
	var discountMoney: Long = 0,

	/**
	 * 原价(划价后价格)
	 */
	@TableField("order_detail_sum_money")
	var orderDetailSumMoney: Long = 0,


	/**
	 * 应付金额（总额）
	 */
	@TableField("original_price")
	var originalPrice: Long = 0,

	/**
	 * 实际付款
	 */
	@TableField("amount_money")
	var amountMoney: Long = 0,

	/**
	 * 就诊记录id
	 */
	@TableField("record_id")
	var recordId: Long = 0,


	/**
	 * 备注
	 */
	@TableField("remark")
	var remark: String? = null,

	/**
	 *
	 */
	@TableField("other")
	var other: Any? = null,

	/**
	 * 整单折扣（手动输入）
	 */
	@TableField("discount")
	var discount: Long = 10000,

	/**
	 * 手动输入的总价（手动折扣用）
	 */
	@TableField("order_manual_sum_money")
	var orderManualSumMoney:Long ?= null,

	@TableField("order_time")
	var orderTime: Date? = null,

	@TableField("first_pay_time")
	var firstPayTime: Date? = null,

	/**
	 * 明细
	 */
	@TableField(exist = false)
	var details: MutableList<PayCustomerOrderDetail>? = null,

	/**
	 * 详情的医生名字
	 */
	@TableField(exist = false)
	var detailsDoctorNames: String? = null,

	/**
	 * 使用的优惠卷id
	 */
	@TableField(exist = false)
	var useCouponsIds: List<Long>? = null,

	/**
	 * 使用的优惠卷id
	 */
	@TableField(exist = false)
	var useCoupons: MutableList<PayCustomerCoupons>? = null,

	/**
	 * 项目优惠使用情况
	 */
	@TableField(exist = false)
	var itemDiscountLogs: MutableList<PayCustomerOrderItemDiscountRuleLog>? = null,

	/**
	 * 订单优惠使用情况
	 */
	@TableField(exist = false)
	var payCustomerOrderDiscountLog: MutableList<PayCustomerOrderDiscountLog>? = null,

	@TableField(exist = false)
	var useGold: Long? = null,

	@TableField("import_order_num")
	var importOrderNum:String ?= null,
) : CustomerLeftJoinInfo() {

	/**
	 * 原总价
	 */
	@TableField(exist = false)
	var orderDetailSumMoneyShow: BigDecimal? = null
		get() {

			if (field == null) {
				return BigDecimal(orderDetailSumMoney.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(
					2,
					RoundingMode.HALF_UP
				)
			}
			return field
		}


	/**
	 * 应付金额
	 */
	@TableField(exist = false)
	var originalPriceShow: BigDecimal? = null
		get() {

			if (field == null) {
				return BigDecimal(originalPrice.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(
					2,
					RoundingMode.HALF_UP
				)
			}
			return field
		}
		set(value) {
			if (value != null) {
				originalPrice = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
				field = value
			}
		}

	/**
	 * 实际付款
	 */
	@TableField(exist = false)
	var amountMoneyShow: BigDecimal? = null
		get() {

			if (field == null) {
				return BigDecimal(amountMoney.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(
					2,
					RoundingMode.HALF_UP
				)
			}
			return field
		}
		set(value) {
			if (value != null) {
				amountMoney = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
				field = value
			}
		}



	/**
	 * 折扣显示
	 */
	@TableField(exist = false)
	var discountShow: BigDecimal? = null
		get() {

			if (field == null) {
				return BigDecimal(discount.div(Constants.OtherConfig.DISCOUNT_TO_DB_DOUBLE)).setScale(
					2,
					RoundingMode.HALF_UP
				)
			}
			return field
		}
		set(value) {
			if (value != null) {
				discount = (value.multiply(BigDecimal(Constants.OtherConfig.DISCOUNT_TO_DB_DOUBLE))).toLong()
				field = value
			}
		}

	@TableField(exist = false)
	var useGoldShow: BigDecimal? = null
		get() {

			if (field == null) {
				return if(useGold == null){
					BigDecimal(0L.div(Constants.OtherConfig.DISCOUNT_TO_DB_DOUBLE)).setScale(
						2,
						RoundingMode.HALF_UP
					)
				}else{
					BigDecimal(useGold!!.div(Constants.OtherConfig.DISCOUNT_TO_DB_DOUBLE)).setScale(
						2,
						RoundingMode.HALF_UP
					)
				}

			}
			return field
		}
		set(value) {
			if (value != null) {
				useGold = (value.multiply(BigDecimal(Constants.OtherConfig.DISCOUNT_TO_DB_DOUBLE))).toLong()
				field = value
			}
		}

	fun getArrears(): BigDecimal? {
		if (originalPriceShow != null && amountMoneyShow != null) {
			return originalPriceShow!!.subtract(amountMoneyShow)
		}
		return null
	}


	/**
	 * 添加使用的优惠卷
	 */
	fun addUseCoupons(coupons: PayCustomerCoupons) {
		if (useCoupons == null) {
			useCoupons = mutableListOf()
		}
		useCoupons!!.add(coupons)
	}


	/**
	 * 添加使用的项目优惠
	 */
	fun addPayCustomerOrderItemDiscountRuleLog(log: PayCustomerOrderItemDiscountRuleLog) {
		if (itemDiscountLogs == null) {
			itemDiscountLogs = mutableListOf()
		}
		itemDiscountLogs!!.add(log)
	}

	fun addPayCustomerOrderDiscountLog(log: PayCustomerOrderDiscountLog) {
		if (payCustomerOrderDiscountLog == null) {
			payCustomerOrderDiscountLog = mutableListOf()
		}
		payCustomerOrderDiscountLog!!.add(log)
	}
}



